package controller;

import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.FileInformation;
import pojo.User;
import service.File.FileService;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author HZY
 */
@Controller
@RequestMapping("/fileService")
public class fileServiceController {
    @Resource
    private FileService fileService;

    /**
     * 创建文档文件
     *
     * @param user
     * @param fileName
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("/createFile")
    public String createFile(
            @RequestParam(value = "User") User user,
            @RequestParam(value = "FileName") String fileName,
            Model model) throws IOException {
        FileInformation fileInformation = new FileInformation(fileName, user.getId(), user.getUsername());
        fileService.createFile(fileInformation);
        Path file = Path.of("E:\\java\\Spring\\lightblog\\file/" + user.getId() + "/" + fileInformation.getFileId() + ".blog");
        Files.copy(Path.of("E:\\java\\Spring\\lightblog\\file/File_templet.blog"), file);
        userServiceController.addEndLog(user.getId(),0,fileInformation.getFileId());
        model.addAttribute("FileInformation", fileInformation);
        return "/DocumentView/ReviseFile";
    }

    /**
     * 修改文档文件
     *
     * @param userId
     * @param fileId
     * @param text
     * @throws IOException
     */
    @RequestMapping("/reviseFile")
    public void reviseFile(
            @RequestParam(value = "UserId") int userId,
            @RequestParam(value = "FileId") int fileId,
            @RequestParam(value = "FileLabel") String newFileLabel,
            @RequestParam(value = "FileName") String newFileName,
            @RequestParam(value = "Text") String text) throws IOException {
        if (newFileLabel != null) {
            reviseFileLabel(fileId, newFileLabel);
        }
        File file = new File("E:\\java\\Spring\\lightblog\\file/" + userId + "/" + fileId + ".blog");
        Files.delete(file.toPath());
        Files.writeString(file.toPath(), text);
        userServiceController.addEndLog(userId,1,fileId);
    }

    /**
     * 修改文档设置（修改标签）
     *
     * @param fileId
     * @param newFileLabel
     * @throws IOException
     */
    public void reviseFileLabel(int fileId, String newFileLabel) throws IOException {
        fileService.reviseFileLabel(fileId, newFileLabel);
    }

    /**
     * 删除文档文件
     *
     * @param userId
     * @param fileId
     * @throws IOException
     */
    @RequestMapping("/deleteFile")
    public void deleteFile(
            @RequestParam(value = "UserId") int userId,
            @RequestParam(value = "FileId") int fileId) throws IOException {
        File file = new File("E:\\java\\Spring\\lightblog\\file/" + userId + "/" + fileId + ".blog");
        Files.delete(file.toPath());
        fileService.deleteFile(fileId);
        userServiceController.addEndLog(userId,2,fileId);
    }

    /**
     * 查看文档
     *
     * @param fileId
     * @param model
     */
    @RequestMapping("/showFile")
    public String showFile(
            @RequestParam(value = "UserId") int userId,
            @RequestParam(value = "FileId") int fileId,
            Model model) throws IOException {
        FileInformation fileInformation = fileService.getFile(fileId);
        Path path = Path.of("E:\\java\\Spring\\lightblog\\file/" + userId + "/" + fileId + ".blog");
        String text = Files.readString(path);
        model.addAttribute("text", text);
        return "/DocumentView/showFile";
    }
//    搜索文档
}
